import requests

from comm.yaml_tools import YamlTools
from conf.env import URL
from comm.log_tools import log

class BaseRequest:
    # 定义类变量 sess
    sess = requests.session()
    # 定义发生请求的方法
    @classmethod
    def send_request(cls, **kwargs):
        try:
            rp = cls.sess.request(**kwargs)
        except Exception as e:
            log.error("接口请求失败")
        else:
            return rp
    @classmethod
    def execute_api_request(cls, request, parametriae):
        method = request["method"]
        cookies = request["cookies"]
        headers = request["headers"]
        url = URL+request["url"]
        data = parametriae["data"]
        params = parametriae["params"]
        json = parametriae["json"]
        files = parametriae["files"]
        expect = parametriae["expect"]
        rp = cls.send_request(method=method,
                              url=url,
                              headers=headers,
                              cookies=cookies,
                              data=data,
                              json=json,
                              params=params,
                              files=files
                              )
        return rp
if __name__ == '__main__':
    login_yaml_data = YamlTools.read_yaml_file("api_login_data.yaml")[0]
    rp = BaseRequest.execute_api_request(login_yaml_data["request"],login_yaml_data["parametrize"][0])
    print(rp.text)